package com.lv.service.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.LayoutRes;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.lv.service.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者： ygx
 * 创建日期：2023/6/6 10:32 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * <p>
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：通用图片选择  布局需要 iv_img
 */
public class GridImageSubAdapter extends
        RecyclerView.Adapter<GridImageSubAdapter.ViewHolder> {
    private LayoutInflater mInflater;
    private int mLayout;
    private List<String> list = new ArrayList<>();

    public GridImageSubAdapter(Context context, @LayoutRes int layout) {
        mInflater = LayoutInflater.from(context);
        this.mLayout = layout;
    }

    public void setList(List<String> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public List<String> getData() {
        return list;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView mImg;

        public ViewHolder(View view) {
            super(view);
            mImg = view.findViewById(R.id.iv_img);
        }
    }

    @Override
    public int getItemCount() {

        return list.size();
    }


    /**
     * 创建ViewHolder
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(mLayout,
                viewGroup, false);
        final ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }


    /**
     * 设置值
     */
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int position) {

        String media = list.get(position);
        Glide.with(viewHolder.itemView.getContext())
                .load(media)
                .centerCrop()
                .placeholder(com.lv.common.R.color.color_f8f8f8)
//                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(viewHolder.mImg);
//            Glide.with(viewHolder.itemView.getContext())
//                    .load(path)
////                    .apply(options)
//                    .into(viewHolder.mImg);
//            }
        //itemView 的点击事件
        if (mItemClickListener != null) {
            String finalPath = media;
            viewHolder.itemView.setOnClickListener(v -> {
                int adapterPosition = viewHolder.getAdapterPosition();
                mItemClickListener.onItemClick(adapterPosition, v, finalPath);
            });
        }

    }

    protected OnItemClickListener mItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(int position, View v, String path);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mItemClickListener = listener;
    }

}
